#import "ALEntry+DisplayAdditions.h"
#import "ALElement+DisplayAdditions.h"


#pragma mark -
#pragma mark Class Globals
static NSArray *ALEntryElementSortDescriptors;
static NSAttributedString *ALEntryNewLineString;
static BOOL ALEntryClassGlobalsInitialized;


#pragma mark -
@interface ALEntry (DisplayAdditionsPrivateMethods)
-(void)initializeClassGlobals;
@end


#pragma mark -
@implementation ALEntry (DisplayAdditions)

-(void)initializeClassGlobals {
	ALEntryElementSortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:ALLineNumberKey ascending:YES]];
	ALEntryNewLineString = [[NSAttributedString alloc] initWithString:@"\n\n"];
	ALEntryClassGlobalsInitialized = YES;
}

-(NSAttributedString *)attributedStringForDisplay {
	if (!ALEntryClassGlobalsInitialized)
		[self initializeClassGlobals];
	NSMutableAttributedString *entryString = [[NSMutableAttributedString alloc] initWithString:@""];
	NSArray *sortedElements = [[self elements] sortedArrayUsingDescriptors:ALEntryElementSortDescriptors];
	for (ALElement *element in sortedElements) {
		NSAttributedString *elementString = [element attributedStringForDisplay];
		if (!elementString)
			continue;
		[entryString appendAttributedString:elementString];
		[entryString appendAttributedString:ALEntryNewLineString];
	}
	return entryString;
}

@end
